//通用元素 node
const anodeScript = require("../../elms/anode");
const pools = {
    "pool-one-node" : {
        _d : null,
        get(){
            this._d = this._d || new cc.NodePool("pool-one-node" );
            !this._d.idd && (this._d.idd = $T.random(1000,10000));
            return this._d;
        },
        getNewNode(){
            let node = new cc.Node("oneNode");
            return node;
        }
    },
    "pool-zidan" : {
        _d : null,
        get( scrcom ){
            this._d = this._d || new cc.NodePool( scrcom || "pool-zidan" );
            !this._d.idd && (this._d.idd = $T.random(1000,10000));
            return this._d;
        }
    },

    "pool-smzidan" : {
        _d : null,
        get( scrcom ){
            this._d = this._d || new cc.NodePool( scrcom || "pool-smzidan" );
            return this._d;
        }
    },

    "pool-virus" : {
        _d : null,
        get(){
            this._d = this._d || new cc.NodePool( );
            return this._d;
        },
        getNewNode(){
            let node = $RES.createPrefab("fab-virus");
            return node;
        }
    },

    "pool-gun" : {
        _d : null,
        get(){
            this._d = this._d || new cc.NodePool( );
            return this._d;
        },
        getNewNode(){
            let node = $RES.createPrefab("fab-gun");
            return node;
        }
    },

    "pool-anode" : {
        _d : null,
        get( scrcom ){
            this._d = this._d || new cc.NodePool( scrcom || "pool-anode" );
            return this._d;
        },
        getNewNode(){
            let node = new cc.Node("anodeEls");
            node.addComponent(anodeScript);
            return node;
        }
    },

    "pool-jingbi" : {
        _d : null,
        get( scrcom ){
            this._d = this._d || new cc.NodePool( scrcom || "pool-jingbi" );
            return this._d;
        }
    },

    poolGetByName : {
        get(name){
            let pool = null;
            if( pools['pool-'+name]){
                pool =  $G.get('pool-'+name);//pools['pool-'+name].get ? pools['pool-'+name].get() : pools['pool-'+name];
            }
            let node = pool ? pool.get() : null;

            if(!node && pools['pool-'+name] && pools['pool-'+name]["getNewNode"] ){
                node = $G.do('pool-'+name , "getNewNode");
            }

            return node;
        }
    },

    poolPutByNode : {
        put( o , name ){
            if(!name){
                let scr = $T.getScr( o );
                let reg = new RegExp(/\<(.*)\>/g);
                let re = reg.exec( scr ? scr.name : "");
                name = re && re[1] ? re[1] : "";
            }

            if(name && pools[ 'pool-'+name ] ){
                let pool = $G.get('pool-'+ name);//pools[ 'pool-'+re[1] ].get();
                pool.put(o);
            }
        }
    },

    oneNode : {
        __pools : {},
        get( name ){
            let pool;
            if(name){
                this.__pools = this.__pools || {};
                this.__pools[name] = this.__pools[name] || new cc.NodePool( name );
                pool = this.__pools[name];
            }else{
                pool = pools["pool-one-node"].get();
            }
            let node = pool.get();
            if(name && !node){
                node = this.getNode( name );
            }else if(!node && pools["pool-one-node"]["getNewNode"] ){
                node = $G.do("pool-one-node" , "getNewNode");
            }

            return node;
        },

        getNode( name ){
            name = name || "oneNode"
            let node = new cc.Node( name );
            return node;
        },

        put( node , name ){
            let pool = null;
            if(name){
                this.__pools = this.__pools || {};
                pool = this.__pools[name];
            }else if(pools["pool-one-node"]){
                pool = pools["pool-one-node"].get();
            }
            pool && pool.put( node );
            //console.log(pool);
        }
    }
} 
 
module.exports = pools; 